<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <body>

        <ui:composition template="./moduleControl.xhtml">

            <ui:define name="tabContent">
                <h:form id="editForm">
                    <p:panel style="width: 500px">
                        <h:panelGrid columns="2" id="editPanel">
                            Tên Module*    
                            <p:inputText value="#{moduleControlBean.selected.moduleName}" required="true" requiredMessage="Tên module không được để trống"/>
                            Lệnh     
                            <p:selectOneMenu value="#{moduleControlBean.selected.command}">
                                <f:selectItem itemLabel="unpause" itemValue="unpause" />
                                <f:selectItem itemLabel="pause" itemValue="pause"/>
                                <f:selectItem itemLabel="reload" itemValue="reload" />
                                <f:selectItem itemLabel="stop" itemValue="stop"/>
                                <f:selectItem itemLabel="wake_up" itemValue="wakeup"/>
                            </p:selectOneMenu>
                            <p:focus context="editPanel"/> 

                        </h:panelGrid>

                        <h:panelGrid columns="5">
                            <p:commandButton update=":tableForm:table" value="Thêm mới" rendered="#{!moduleControlBean.edit}" action="#{moduleControlBean.create()}"/>
                            <p:commandButton update=":tableForm:table" value="Cập nhật" rendered="#{moduleControlBean.edit}" action="#{moduleControlBean.edit()}"/>
                            <p:commandButton update=":tableForm:table" value="Hủy bỏ" immediate="true" action="#{moduleControlBean.resetToCreateForm()}"/>
                        </h:panelGrid>
                    </p:panel>
                </h:form>
                <h:form id="tableForm">
                    <p:poll id="poll" listener="#{moduleControlBean.reloadList()}" interval="5" update="table" />
                    <p:dataTable id="table" value="#{moduleControlBean.list}" var="module" style="width: 600px"
                                 rows="20" paginator="#{moduleControlBean.list.size() > 20}" rowKey="#{module.moduleId}"
                                 selection="#{moduleControlBean.selectedList}" selectionMode="multiple">

                        <f:facet name="header">  
                            <div align="left">
                                <p:menuButton value="Xử lý bản ghi được chọn">  
                                    <p:menuitem update=":editForm:editPanel" value="Chỉnh sửa" icon="ui-icon ui-icon-pencil" action="#{moduleControlBean.prepareEdit()}"/>
                                    <p:menuitem value="Xóa" icon="ui-icon ui-icon-close" onclick="moduleDeleteConfirm.show()" />   
                                </p:menuButton> 
                            </div>
                        </f:facet> 

                        <f:facet name="footer">  
                            <div align="left">
                                <p:menuButton value="Xử lý bản ghi được chọn">  
                                    <p:menuitem update=":editForm:editPanel" value="Chỉnh sửa" icon="ui-icon ui-icon-pencil" action="#{moduleControlBean.prepareEdit()}"/>
                                    <p:menuitem value="Xóa" icon="ui-icon ui-icon-close" onclick="moduleDeleteConfirm.show()" />   
                                </p:menuButton> 
                            </div>
                        </f:facet> 

                        <p:column headerText="Module" sortBy="#{module.moduleName}">
                            #{module.moduleName}
                        </p:column>
                        <p:column headerText="Lệnh hiện tại" sortBy="#{module.command}">
                            #{module.command}
                        </p:column>
                        <p:column headerText="Ra lệnh mới" >
                            <p:commandButton ajax="false" title="Unpause" icon="ui-icon ui-icon-play" action="#{moduleControlBean.issueCommand()}">
                                <f:param name="id" value="#{module.moduleId}"/>
                                <f:param name="command" value="unpause"/>
                            </p:commandButton>
                            <p:commandButton ajax="false" title="Pause" icon="ui-icon ui-icon-pause" action="#{moduleControlBean.issueCommand()}">
                                <f:param name="id" value="#{module.moduleId}"/>
                                <f:param name="command" value="pause"/>
                            </p:commandButton>
                            <p:commandButton ajax="false" title="Reload" icon="ui-icon ui-icon-refresh" action="#{moduleControlBean.issueCommand()}">
                                <f:param name="id" value="#{module.moduleId}"/>
                                <f:param name="command" value="reload"/>
                            </p:commandButton>
                            <p:commandButton ajax="false" title="Stop" icon="ui-icon ui-icon-stop" action="#{moduleControlBean.issueCommand()}">
                                <f:param name="id" value="#{module.moduleId}"/>
                                <f:param name="command" value="stop"/>
                            </p:commandButton>
                        </p:column>
                    </p:dataTable>

                    <p:confirmDialog message="Bạn muốn xóa các bản ghi đã chọn?"  
                                     header="Xác nhận" severity="alert" widgetVar="moduleDeleteConfirm">  

                        <p:commandButton value="Có" update="table" oncomplete="moduleDeleteConfirm.hide()"  
                                         action="#{moduleControlBean.remove()}"/>
                        <p:commandButton value="Không" onclick="moduleDeleteConfirm.hide()" type="button" />   

                    </p:confirmDialog> 
                </h:form>
            </ui:define>

        </ui:composition>

    </body>
</html>
